package com.yuzhitong.shapi.presenter;

import com.alibaba.fastjson.JSON;
import com.yuzhitong.shapi.BuildConfig;
import a.a.a.MyApplication;
import com.yuzhitong.shapi.base.BasePresenter;
import com.yuzhitong.shapi.base.Contents;
import com.yuzhitong.shapi.base.net.BaseListBean;
import com.yuzhitong.shapi.base.net.BaseObjectBean;
import com.yuzhitong.shapi.bean.CategoryBean;
import com.yuzhitong.shapi.bean.MovieBean;
import com.yuzhitong.shapi.bean.UpdateBean;
import com.yuzhitong.shapi.contract.MovieContract;
import com.yuzhitong.shapi.contract.UpdateContract;
import com.yuzhitong.shapi.model.MovieListModel;
import com.yuzhitong.shapi.model.MovieModel;
import com.yuzhitong.shapi.model.UpdateModel;
import com.yuzhitong.shapi.net.MyObserver;
import com.yuzhitong.shapi.net.RxScheduler;
import com.yuzhitong.shapi.util.LoggerUtil;

import java.util.Map;

import io.reactivex.rxjava3.annotations.NonNull;

/**
 * Created by Brsya
 * CreateDate: 2022/4/29  18:04
 * Description:
 * 版本更新
 */
public class UpdatePresenter extends BasePresenter<UpdateContract.View> implements UpdateContract.Presenter {
    private UpdateContract.Model model;
    private MovieContract.Model movieModel;
    private MovieListModel listModel;

    public UpdatePresenter() {
        model = new UpdateModel();
        movieModel = new MovieModel();
        listModel = new MovieListModel();
    }

    @Override
    public void getUpdateLog() {
        Map<String, Object> par = getPublicPar();

        model.getUpdateLog(par)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new MyObserver<BaseObjectBean<UpdateBean>>(mView) {
                    @Override
                    public void success(BaseObjectBean<UpdateBean> baseBean) {
                        baseBean.handleData(UpdateBean.class);

                        LoggerUtil.e("更新接口" + JSON.toJSONString(baseBean.getData()));
                        if (baseBean.getData() == null) {
                            return;
                        }
                        int versionNumber = baseBean.getData().getVersionNumber();
                        if (versionNumber > BuildConfig.VERSION_CODE){
                            mView.showUpdateDialog(baseBean.getData());
                        }
                    }
                });
    }


    @Override
    public void getCategory() {
        MyApplication.aCache.remove(Contents.CACHE_KEY_CATEGORY);
        movieModel.getCategory(getPublicPar())
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new MyObserver<BaseListBean<CategoryBean>>(mView) {
                    @Override
                    public void success(@NonNull BaseListBean<CategoryBean> baseBean) {
                        baseBean.handleData(CategoryBean.class);
                        if (baseBean.getData() != null && baseBean.getData().size() > 0) {
                            MyApplication.aCache.put(Contents.CACHE_KEY_CATEGORY, JSON.toJSONString(baseBean.getData()));
                            String type = null;
                            String hotKey = null;
                            for (CategoryBean categoryBean : baseBean.getData()) {
                                if ("type".equals(categoryBean.getCode())) {
                                    if (categoryBean.getChail() != null && categoryBean.getChail().size() > 0) {
                                        type = categoryBean.getChail().get(0).getCode();
                                    }
                                    continue;
                                }
                                if ("sort".equals(categoryBean.getCode())) {
                                    if (categoryBean.getChail() != null && categoryBean.getChail().size() > 0) {
                                        hotKey = categoryBean.getChail().get(0).getCode();
                                    }
                                }
                            }
                            if (type != null) {
                                getDayHot(type);
                                if (hotKey != null) {
                                    getHot(type, hotKey);
                                }
                            }
                        }
                    }
                });
    }

    @Override
    public void getHot(String type, String sort) {
        MyApplication.aCache.remove(Contents.CACHE_KEY_CATEGORY_HOT);

        Map<String, Object> par = getPublicPar();
        par.put("type", type);


        listModel.getHot(par)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new MyObserver<BaseListBean<MovieBean>>(mView) {
                    @Override
                    public void success(@NonNull BaseListBean<MovieBean> baseBean) {
                        baseBean.handleData(MovieBean.class);
                        if (baseBean.getData() != null && baseBean.getData().size() > 0) {
                            MyApplication.aCache.put(Contents.CACHE_KEY_CATEGORY_HOT, JSON.toJSONString(baseBean.getData()));
                        }
                    }
                });

    }

    @Override
    public void getDayHot(String type) {
        MyApplication.aCache.remove(Contents.CACHE_KEY_CATEGORY_DAY);

        Map<String, Object> par = getPublicPar();
        par.put("type", type);

        listModel.getDayHot(par)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new MyObserver<BaseListBean<MovieBean>>(mView) {
                    @Override
                    public void success(@NonNull BaseListBean<MovieBean> baseBean) {
                        baseBean.handleData(MovieBean.class);
                        if (mView != null) {
                            if (baseBean.getData() != null && baseBean.getData().size() > 0) {
                                MyApplication.aCache.put(Contents.CACHE_KEY_CATEGORY_DAY, JSON.toJSONString(baseBean.getData()));
                            }
                        }
                    }
                });
    }
}
